<template>
    <div>

        <el-form :model="loginInfo" status-icon :rules="rules2" ref="loginInfo" label-width="100px" class="loginForm">
            <h2 style="padding-left: 80px">欢迎登录</h2>
            <el-form-item prop="username">
                <el-input type="text" v-model="loginInfo.username" auto-complete="off" class="loginInput"
                          placeholder="用户名"></el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input type="password" v-model="loginInfo.password" auto-complete="off" class="loginInput"
                          placeholder="密码"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" class="loginBtn" @click="submitForm('loginInfo')">登录</el-button>
                <div class="footer-tip" @click="toRegister">
                    没有账号？直接注册
                </div>
            </el-form-item>
        </el-form>

    </div>
</template>

<script>
    import fetch from '../api/fetch'

    export default {
        name: "Login",
        data() {
            var validUsername = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入用户名'))
                } else {
                    callback()
                }
            };
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'))
                } else {
                    callback()
                }
            };
            return {
                loginInfo: {
                    password: '',
                    username: ''
                },
                rules2: {
                    username: [{validator: validUsername, trigger: 'blur'}],
                    password: [{validator: validatePass, trigger: 'blur'}]
                }
            }
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate(valid => {
                    if (valid) {
                        fetch
                            .login(this.loginInfo)
                            .then(res => {
                                // console.log(res.data.status);
                                if (res.data.status === 200) {
                                    // console.log(res.data.data.token);
                                    localStorage.setItem('token', res.data.data.token);
                                    this.$router.push({name: 'Home'})

                                }
                                else if (res.data.status === 400) {
                                    // console.log(res.data);
                                    this.$message({
                                        message: "账号或密码有误",
                                        type: 'danger'
                                    })
                                }
                            })
                            .catch(e => {
                                console.log(e)
                            })
                    }
                })
            },
            toRegister() {
                this.$router.push({name: 'Register'})
            },
        }
    }


</script>

<style scoped>
    @import "../assets/Animate/animate.min.css";

    body {
        padding: 0;
        margin: 0;
    }

    * {
        box-sizing: border-box;
    }

    .loginForm {
        padding: 100px 500px;
    }

    .loginInput {
        width: 100%;
    }

    .footer-tip {
        color: rgba(0, 0, 0, 0.5);
        font-size: 16px;
        padding-bottom: 20px;
        cursor: pointer;
        bottom: 16px;
        right: 16px;
    }

    .loginBtn {
        width: 100%;
    }
</style>